﻿$executingScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. "$executingScriptDir\PsUtils.ps1"

function RebuildConfigureTree($buildConfiguration, $xmlData)
{
	$config = $xmlData.NUnitProject.Config | ? { $_.name -eq $buildConfiguration }

	$childrenClone = @()
	$config.ChildNodes | % { $childrenClone += $_ }
	$childrenClone | % { $config.RemoveChild($_) }

	Get-ChildItem -Recurse -Path $pathSource -Filter "*.Tests.dll" | % { 
		if(($_.FullName.Contains("obj") -eq $false) -and ($_.FullName.Contains($buildConfiguration) -eq $true))
		{
			$assemNode = $data.CreateElement("assembly") 
			$dirName = Resolve-Path -Relative $_.Directory 
			$filename = $_.Name
			$fullPath = Join-Path ($dirName.Substring(2)) $filename
			$assemNode.SetAttribute("path", $fullPath) 
			$config.AppendChild($assemNode)
		}
	}
}

$file = "$pathSource\GlobalUnitTests.nunit"

$data = [XML] (Get-Content $file)
$data.NUnitProject.Settings.SetAttribute("activeconfig", "Debug")

Set-Location $pathSource
RebuildConfigureTree "Debug" $data

Set-Content -Value $data.OuterXml -Path $file